package com.atguigu.eduorder.service.impl;

import com.atguigu.commonutils.ordervo.CourseWebOrder;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduorder.client.EduClient;
import com.atguigu.eduorder.client.UcenterClient;
import com.atguigu.eduorder.entity.Order;
import com.atguigu.eduorder.mapper.OrderMapper;
import com.atguigu.eduorder.service.OrderService;
import com.atguigu.eduorder.utils.OrderNoUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.OrderUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author wxc
 * @since 2021-07-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private EduClient eduClient;

    @Autowired
    private UcenterClient ucenterClient;

    /**
     * 创建订单，返回订单号
     *
     * @param courseId
     * @param memberId
     **/
    @Override
    public String createOrders(String courseId, String memberId) {
        //通过远程调用根据用户id获取用户信息
        UcenterMemberOrder userInfo = ucenterClient.getUserInfo(memberId);

        //通过远程调用根据课程id获取客户才能信息
        CourseWebOrder courseInfoOrder = eduClient.getCourseInfoOrder(courseId);

        //创建order对象，向order对象里面设置需要数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo()); //订单号
        order.setCourseId(courseId);    //课程id
        order.setCourseTitle(courseInfoOrder.getTitle()); //课程标题
        order.setCourseCover(courseInfoOrder.getCover()); //课程封面
        order.setTeacherName(courseInfoOrder.getTeacherName());   //讲师信息
        order.setTotalFee(courseInfoOrder.getPrice());    //订单金额
        order.setMemberId(memberId);    //用户id
        order.setMobile(userInfo.getMobile()); //用户手机号
        order.setNickname(userInfo.getNickname()); //用户昵称
        order.setStatus(0); //订单状态 0未支付 1已支付
        order.setPayType(1);    //订单支付类型，默认为 微信1


        baseMapper.insert(order);

        return order.getOrderNo();
    }
}
